package com.fengmi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsCat;
import com.fengmi.entity.ViewCatTemplate;
import com.fengmi.mapper.CatTemplateMapper;
import com.fengmi.service.ICatTemplateService;
import com.fengmi.vo.CatCascadeVo;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者：陈学虎
 * 时间：2021/7/10 0010 下午 2:34
 */
@Service
public class CatTemplateService implements ICatTemplateService {

    @Autowired
    private CatTemplateMapper catTemplateMapper;

    @Override
    public PageResultVO<ViewCatTemplate> findByParen(PageDTO<ViewCatTemplate> pageDTO) {
        if (ObjectUtil.isEmpty(pageDTO)){
            return new PageResultVO<>(false,"请求参数错误");
        }

        PageHelper.startPage(pageDTO.getPage(),pageDTO.getSize());

        List<ViewCatTemplate> catTemplates = catTemplateMapper.findByParen(pageDTO.getWhere().getParentId());

        PageInfo<ViewCatTemplate> pageInfo = new PageInfo<>(catTemplates);

        PageResultVO<ViewCatTemplate> pageResultVO = new PageResultVO<>(true,"查询成功");

        pageResultVO.setData(pageInfo.getList());
        pageResultVO.setTotal(pageInfo.getTotal());
        pageResultVO.setPages(pageInfo.getPages());
        return pageResultVO;
    }

    @Override
    public ResultVO addCat(MallGoodsCat mallGoodsCat) {
        if (ObjectUtil.isEmpty(mallGoodsCat)){
            return new ResultVO(false,"请求参数错误");
        }

        int i = catTemplateMapper.addCat(mallGoodsCat);
        return new ResultVO(i==1?true:false,i==1?"保存成功":"保存失败");
    }

    @Override
    public List<CatCascadeVo> findCatCascade() {
        return catTemplateMapper.findCatCascade();
    }
}
